package com.aduerr.v1shouyin.mvp.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.mvp.activity.OrderInquireListActivity;
import com.aduerr.v1shouyin.util.DayWeekMoonTImeUtil;
import com.aduerr.v1shouyin.util.SerializableMap;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.aduerr.v1shouyin.view.pickerview.TimePicker2View;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 类名: AdvanceFilterFregment
 * 此类用途: ---
 *
 * @Author: peixi
 * @Date: 2017-12-08 20:52
 * @Email: cr7inmanchester.com
 * @FileName: com.aduer.shouyin.mvp.fragment.AdvanceFilterFregment.java
 */
public class AdvanceFilterFregment extends Fragment {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.all_type)
    TextView all_type;
    @BindView(R.id.zhifubao)
    TextView zhifubao;
    @BindView(R.id.weixin)
    TextView weixin;
    @BindView(R.id.qq)
    TextView qq;
    @BindView(R.id.cash)
    TextView cash;
    @BindView(R.id.other)
    TextView other;
    @BindView(R.id.all)
    TextView all;
    @BindView(R.id.weizhifu)
    TextView weizhifu;
    @BindView(R.id.yizhifu)
    TextView yizhifu;
    @BindView(R.id.yituikaun)
    TextView yituikaun;
    @BindView(R.id.bufentuikaun)
    TextView bufentuikaun;
    @BindView(R.id.jiaoyiguanbi)
    TextView jiaoyiguanbi;
    @BindView(R.id.tv_serch)
    TextView tvSerch;
    Unbinder unbinder;
    //装入参数
    Map<String, String> addprem = new HashMap<>();
    private String yestdayBeginTime;
    private String yesdayEndTime;
    private TimePicker2View pvTime2;
    private Date beginDate;
    private Date endDate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.advancefilter_fregment, null, false);
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTimePicker();
        initUI();
    }

    private void initUI() {
        //时间控件
        yestdayBeginTime = DayWeekMoonTImeUtil.getYestdayBeginTime();
        String[] split = yestdayBeginTime.split(":");
        yesdayEndTime = DayWeekMoonTImeUtil.getYesdayEndTime();
        String[] split1 = yesdayEndTime.split(":");
        tvTime.setText(split[0] + ":" + split[1] + "至" + split1[0] + ":" + split1[1]);

        all_type.setSelected(true);
        all.setSelected(true);

        addprem.put("begindate", yestdayBeginTime);
        addprem.put("enddate", yesdayEndTime);
        addprem.put("paytype", "0");
        addprem.put("state", "0");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_time, R.id.zhifubao, R.id.weixin, R.id.qq, R.id.cash, R.id.other,
            R.id.all, R.id.weizhifu, R.id.yizhifu, R.id.yituikaun, R.id.bufentuikaun,
            R.id.jiaoyiguanbi, R.id.tv_serch, R.id.all_type})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.tv_time:
                pvTime2.show();
                break;
            case R.id.all_type:
                clearAllPaytype();
                all_type.setSelected(true);
                addprem.put("paytype", "0");
                break;
            case R.id.zhifubao:
                clearAllPaytype();
                zhifubao.setSelected(true);
                addprem.put("paytype", "2");
                break;
            case R.id.weixin:
                clearAllPaytype();
                weixin.setSelected(true);
                addprem.put("paytype", "1");
                break;
            case R.id.qq:
                clearAllPaytype();
                qq.setSelected(true);
                addprem.put("paytype", "3");
                break;
            case R.id.cash:
                clearAllPaytype();
                cash.setSelected(true);
                addprem.put("paytype", "5");
                break;
            case R.id.other:
                clearAllPaytype();
                other.setSelected(true);
                addprem.put("paytype", "4");
                break;
            case R.id.all:
                clearAllPayState();
                all.setSelected(true);
                addprem.put("state", "0");
                break;
            case R.id.weizhifu:
                clearAllPayState();
                weizhifu.setSelected(true);
                addprem.put("state", "3");
                break;
            case R.id.yizhifu:
                clearAllPayState();
                yizhifu.setSelected(true);
                addprem.put("state", "1");
                break;
            case R.id.yituikaun:
                clearAllPayState();
                yituikaun.setSelected(true);
                addprem.put("state", "4");
                break;
            case R.id.bufentuikaun:
                clearAllPayState();
                bufentuikaun.setSelected(true);
                addprem.put("state", "2");
                break;
            case R.id.jiaoyiguanbi:
                clearAllPayState();
                jiaoyiguanbi.setSelected(true);
                addprem.put("state", "5");
                break;
            case R.id.tv_serch:
                //跳转到订单查新的详情界面
                Intent intent = new Intent(getContext(), OrderInquireListActivity.class);
                SerializableMap serializableMap = new SerializableMap();
                serializableMap.setMap(addprem);
                //将map数据添加到封装的myMap中
                Bundle bundle = new Bundle();
                bundle.putSerializable("serializableMap", serializableMap);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    //初始化时间控件
    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(2013, 1, 23);
        final Calendar endCalendar = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取年份
        int month = cal.get(Calendar.MONTH);//获取月份
        int day = cal.get(Calendar.DATE);//获取日
//        endCalendar.set(year, month, day);
        int hour = cal.get(Calendar.HOUR_OF_DAY);//小时
        int minute = cal.get(Calendar.MINUTE);//分
        endCalendar.set(year, month, day, hour, minute);
//        //时间选择器

        pvTime2 = new TimePicker2View.Builder(getActivity(),
                new TimePicker2View.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v, Date date2) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null

                Date curDate = new Date(System.currentTimeMillis());
                if(!date2.before(curDate) || !date.before(curDate)) {//大于当前时间
                    ToastUtils.showToast(getActivity(), "时间不能大于当前时间");
                    return;
                }

                if(!date2.before(date)) {
                    //时间选择器
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String formatstart = sdf.format(date);

                    String[] splitstart = formatstart.split(" ");
//                    timepickerstartdata.setText(splitstart[0]);
//                    timpickerstarttime.setText(splitstart[1]);

                    String formatend = sdf.format(date2);
                    String[] splitend = formatend.split(" ");
//                    enddatatimepicker.setText(splitend[0]);
//                    endtimetimepicker.setText(splitend[1]);


                    StringBuffer stringBuffer = new StringBuffer();
                    String begintime = sdf.format(date);
                    String endtime = sdf.format(date2);
                    stringBuffer.append(begintime);
                    stringBuffer.append("至" + endtime);
                    tvTime.setText(stringBuffer.toString());

                    addprem.put("begindate", begintime + ":00");
                    addprem.put("enddate", endtime + ":59");


                    Calendar c = Calendar.getInstance();
                    c.setTime(date);
                    c.set(Calendar.HOUR_OF_DAY, 0);                  //把当前时间小时变成０
                    c.set(Calendar.MINUTE, 0);                      //把当前时间分钟变成０
                    c.set(Calendar.SECOND, 0);                      //把当前时间秒数变成０
                    c.set(Calendar.MILLISECOND, 0);                 //把当前时间毫秒变成０
                    beginDate = c.getTime();

                    c.setTime(date2);
                    c.set(Calendar.HOUR_OF_DAY, 23);                        //把当前时间小时变成23
                    c.set(Calendar.MINUTE, 59);                      //把当前时间分钟变成59
                    c.set(Calendar.SECOND, 59);                      //把当前时间秒数变成59
                    c.set(Calendar.MILLISECOND, 999);
                    endDate = c.getTime();

                    //// TODO: 2017/12/12 0012  筛选界面


                } else {
                    Toast.makeText(getActivity(),
                            getResources().getString(R.string.pickerview_choosedate_error), Toast.LENGTH_SHORT).show();
                }
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
//                .setType(new boolean[]{true, true, true, false, false, false})
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "")
//                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)
                .setDate(selectedDate, selectedDate)
                .setRangDate(startCalendar, endCalendar, startCalendar, endCalendar)
                .setBackgroundId(0x33000000) //设置外部遮罩颜色
                .setDecorView(null)
                .setTitleText(getResources().getString(R.string.pickerview_choosedate))
                .setTitleBgColor(getResources().getColor(R.color.home_main_color))
                .setTitleColor(getResources().getColor(R.color.white))
                .setSubmitColor(getResources().getColor(R.color.white))
                .setCancelColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(android.R.color.transparent))
                .build();
    }

    private void clearAllPaytype() {
        all_type.setSelected(false);
        zhifubao.setSelected(false);
        weixin.setSelected(false);
        qq.setSelected(false);
        cash.setSelected(false);
        other.setSelected(false);
    }

    private void clearAllPayState() {
        all.setSelected(false);
        weizhifu.setSelected(false);
        yizhifu.setSelected(false);
        yituikaun.setSelected(false);
        yituikaun.setSelected(false);
        bufentuikaun.setSelected(false);
        jiaoyiguanbi.setSelected(false);
    }
}
